package com.dc.bobaoge.project.v3.to.v2.business.core;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.dc.bobaoge.base.entity.core.po.AuthenticationInfo;
import com.dc.bobaoge.base.entity.core.po.AuthenticationInfoHistory;
import com.dc.bobaoge.base.entity.core.po.AuthenticationInfoStore;
import com.dc.bobaoge.base.entity.core.po.AuthenticationInfoUpdate;
import com.dc.bobaoge.base.mapper.core.AuthenticationInfoHistoryMapper;
import com.dc.bobaoge.base.mapper.core.AuthenticationInfoMapper;
import com.dc.bobaoge.base.mapper.core.AuthenticationInfoStoreMapper;
import com.dc.bobaoge.base.mapper.core.AuthenticationInfoUpdateMapper;
import com.dc.bobaoge.project.base.core.constant.AuthInfoConstant;
import com.dc.bobaoge.project.base.core.constant.BaseInfoConstant;
import com.dc.bobaoge.project.base.core.old.entity.OldCoreCerCompany;
import com.dc.bobaoge.project.base.core.old.entity.OldCoreCerLevelSub;
import com.dc.bobaoge.project.base.core.old.entity.OldCoreCerMessage;
import com.dc.bobaoge.project.base.core.old.entity.OldCoreCerPerson;
import com.dc.bobaoge.project.base.core.old.mapper.OldCoreCerCompanyMapper;
import com.dc.bobaoge.project.base.core.old.mapper.OldCoreCerLevelMapper;
import com.dc.bobaoge.project.base.core.old.mapper.OldCoreCerLevelSubMapper;
import com.dc.bobaoge.project.base.core.old.mapper.OldCoreCerMessageMapper;
import com.dc.bobaoge.project.base.core.old.mapper.OldCoreCerPersonMapper;
import com.dc.bobaoge.project.v3.to.v2.business.core.util.AuthenticationInfoUtil;
import com.own.business.sync.trigger.base.entity.SyncTrigger;
import com.own.business.sync.trigger.base.method.ResolveMethod;
import com.own.component.common.exception.BusinessSimpleException;
import com.own.component.common.util.list.ListUtil;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.stream.Collectors;

/**
 * AuthenticationInfoHistoryResolveMethod
 * <p>
 * 可触达的旧版系统审核状态
 * 理论上单个用户只会存在一个审核中的状态
 * 仅判断当前查询到的数据的最新一条数据
 *
 * <p>
 * <table class="striped">
 * <caption>审核状态的触发</caption>
 * <thead>
 *  <tr><th scope="col">目标的审核状态</th><th scope="col">状态ENUM</th><th scope="col">逻辑</th>
 * </thead>
 * <tbody>
 *   <tr><td>①审核中(首次）</td><td>{@link AuthInfoConstant.OldStatus#AUDITING}</td><td>数据的状态为审核中，且没有查询到之前的流水数据</td></tr>
 *   <tr><td>②通过</td><td>{@link AuthInfoConstant.OldStatus#PASS}</td><td>数据的状态为审核通过</td></tr>
 *   <tr><td>③拒绝</td><td>{@link AuthInfoConstant.OldStatus#REJECT}</td><td>数据的状态为审核不通过，且没有查询到之前的流水数据</td></tr>
 *   <tr><td>④拒绝再次提交</td><td>{@link AuthInfoConstant.OldStatus#REJECT_SUBMIT_AGAIN}</td><td>数据的状态为审核中，且查询到上一条的数据为审核不通过</td></tr>
 *   <tr><td>⑤续费付钱之后的状态</td><td>{@link AuthInfoConstant.OldStatus#RENEWED}</td><td>数据的状态为审核中，且查询到上一条的数据为审核通过，且有相同的vipId</td></tr>
 *   <tr><td>⑥续费升级之后的状态</td><td>{@link AuthInfoConstant.OldStatus#UPGRADE}</td><td>数据的状态为审核中，且查询到上一条的数据为审核通过，但有不相同的vipId</td></tr>
 *   <tr><td>⑦续费被拒绝</td><td>{@link AuthInfoConstant.OldStatus#RENEWED_REJECT}</td><td>数据的状态为审核不通过，且查询到上一条的数据为审核通过，且有相同的vipId</td></tr>
 *   <tr><td>⑧升级被拒绝</td><td>{@link AuthInfoConstant.OldStatus#UPGRADE_REJECT}</td><td>数据的状态为审核不通过，且查询到上一条的数据为审核通过，但有不相同的vipId</td></tr>
 * </tbody>
 * </table>
 * <p>
 *
 * @author chenxueli
 * @date 2024-01-27 09:42:59
 */
@SuppressWarnings("all")
@Slf4j
@Component
public class AuthenticationInfoHistoryResolveMethod implements ResolveMethod {

    private static final String DEFAULT_TAG = "厂";
    @Resource
    private AuthenticationInfoHistoryMapper authenticationInfoHistoryMapper;
    @Resource
    private AuthenticationInfoMapper authenticationInfoMapper;

    @Resource
    private OldCoreCerMessageMapper oldCoreCerMessageMapper;
    @Resource
    private AuthenticationInfoUpdateMapper authenticationInfoUpdateMapper;
    @Resource
    private AuthenticationInfoStoreMapper authenticationInfoStoreMapper;

    @Resource
    private OldCoreCerPersonMapper oldCoreCerPersonMapper;
    @Resource
    private OldCoreCerCompanyMapper oldCoreCerCompanyMapper;
    @Resource
    private OldCoreCerLevelMapper oldCoreCerLevelMapper;
    @Resource
    private OldCoreCerLevelSubMapper oldCoreCerLevelSubMapper;

    @Resource
    private AuthenticationInfoUtil authenticationInfoUtil;

    /**
     * 修改的处理方法
     *
     * @param trigger 日志信息
     */
    @Override
    public void update(SyncTrigger trigger) {
        if (trigger.getNewStatus() != null && trigger.getNewStatus() != AuthInfoConstant.NewStatus.PASS) {
            log.debug("不是通过状态，不处理:{},{}", trigger.getNewStatus(), trigger.getTargetId());
            return;
        }
        var history = authenticationInfoHistoryMapper.selectById(trigger.getTargetId());
        if (history == null) {
            throw new BusinessSimpleException("未查询到认证信息");
        }
        if (history.getAuditStatus() != AuthInfoConstant.NewStatus.PASS) {
            log.debug("不是通过状态，不处理:{},{}", history.getAuditStatus(), trigger.getTargetId());
            return;
        }
        // 如果是广告主的认证，直接丢弃
        if (history.getAdvertisementType() != null && history.getAdvertisementType() != 0) {
            log.debug("广告主认证信息：{}", trigger.getTargetId());
            return;
        }
        // 查询之前这个信息是否在旧版服务中就存在了
        var oldCoreCerMessage = oldCoreCerMessageMapper.selectById(trigger.getTargetId());
        if (oldCoreCerMessage == null) {
            // 执行新增的操作
            add(trigger, history);
        } else {
            // 执行修改的操作
            update(trigger, history, oldCoreCerMessage);
        }
    }

    /**
     * 新增的处理方法
     *
     * @param trigger 日志信息
     * @param history 认证信息
     */
    private void add(SyncTrigger trigger, AuthenticationInfoHistory history) {
        var status = calcStatus(history);
        var authInfo = getAuthInfoByUserId(history.getFkUserId());
        var searchId = 0L;
        if (history.getVipType() == 1) {
            var person = new OldCoreCerPerson();
            person.setCerUserId(history.getFkUserId());
            person.setCerUserNickname(history.getNickname());
            person.setCerPersonName(history.getName());
            person.setCerPersonPhone(history.getPhone());
            person.setCerPersonIdcard(history.getCertificateNo());
            person.setCerPersonBank("");
            person.setCerPersonBankcard(StringUtils.isBlank(history.getBankCardNo()) ? "" : history.getBankCardNo());
            person.setCerPersonImg(history.getCertificateFront());
            person.setCerPersonHeadimg(history.getAvatar());
            person.setCerPersonAddress(history.getAddress());
            person.setCerPersonRange(authenticationInfoUtil.resolveBusinessTypeName(history.getRfBusinessTypeName()));
            person.setCerPersonCreatetime(authInfo == null ? history.getGmtCreate() : authInfo.getFirstTime());
            person.setCerPersonLevel(history.getFkVipId() + "");
            person.setCerPersonEdit(editCount(history.getFkUserId(), history.getId()));
            person.setCerPersonPremises("");
            person.setCerPersonCon(history.getCertificateBack());
            person.setCerPersonHand(history.getCertificateHand());
            person.setCerPersonState(status);
            person.setCerPersonDetails("");
            oldCoreCerPersonMapper.insert(person);
            searchId = person.getCerPersonId();
        } else if (history.getVipType() == 2) {
            var company = new OldCoreCerCompany();
            company.setCerUserId(history.getFkUserId());
            company.setCerCompanyName(history.getName());
            company.setCerCompanyNickname(history.getNickname());
            company.setCerCompanyPhone(history.getPhone());
            company.setCerCompanyIdcard(history.getCertificateNo());
            company.setCerCompanyBank("");
            company.setCerCompanyBankcard(StringUtils.isBlank(history.getBankCardNo()) ? "" : history.getBankCardNo());
            company.setCerCompanyImg(history.getCertificateFront());
            company.setCerCompanyHeadimg(history.getAvatar());
            company.setCerCompanyMaster(history.getBusinessLicense());
            company.setCerCompanyAddress(history.getAddress());
            company.setCerCompanyContact(history.getPhone());
            company.setCerCompanyRange(authenticationInfoUtil.resolveBusinessTypeName(history.getRfBusinessTypeName()));
            company.setCerCompanyLevel(history.getFkVipId() + "");
            company.setCerCompanyEdit(editCount(history.getFkUserId(), history.getId()));
            company.setCerCompanyPremises("");
            company.setCerCompanyCon(history.getCertificateBack());
            company.setCerCompanyHand(history.getCertificateHand());
            company.setCerCompanyAgent(history.getLegalName());
            company.setCerCompanyProduct("");
            company.setCerCompanyAgentPhone(history.getLegalPhone());
            company.setCerCompanyAgentFace(history.getLegalCertificateFront());
            company.setCerCompanyAgentCon(history.getLegalCertificateBack());
            company.setCerCompanyAgentHand(history.getLegalCertificateHand());
            company.setCerCompanyIdentical(history.getLegalCertificateFront().equals(history.getCertificateFront()) ? "1" : "2");
            company.setCerCompanyAgentIdcard(history.getLegalCertificateNo());
            if (StringUtils.isNotBlank(history.getOtherLicense())) {
                var list = ListUtil.Spilt.to(history.getOtherLicense(), ",");
                company.setCerCompanyOther(!list.isEmpty() ? list.getFirst() : "");
                company.setCerCompanyOther2(list.size() > 1 ? list.get(1) : "");
            } else {
                company.setCerCompanyOther("");
                company.setCerCompanyOther2("");
            }
            var store = getStoreByUserId(history.getFkUserId());
            if (store != null) {
                company.setCerCompanyProduct(String.join(BaseInfoConstant.IMAGE_SPILT, ListUtil.Spilt.to(store.getProductUrls(), ",")));
                company.setCerCompanyPremises(String.join(BaseInfoConstant.IMAGE_SPILT, ListUtil.Spilt.to(store.getSceneUrls(), ",")));
            }
            company.setCerCompanyHonor("");
            company.setCerCompanyTag(DEFAULT_TAG);
            company.setCerCompanyState(status);
            company.setCerCompanyDetails("");
            oldCoreCerCompanyMapper.insert(company);
            searchId = company.getCerCompanyId();
        }
        // 新增
        var oldCoreCerMessage = new OldCoreCerMessage();
        oldCoreCerMessage.setCerMsgId(history.getId());
        oldCoreCerMessage.setCerUserId(history.getFkUserId());
        oldCoreCerMessage.setCerSearchId(searchId);
        oldCoreCerMessage.setCerMsgNickname(history.getNickname());
        oldCoreCerMessage.setCerMsgPhone(history.getPhone());
        oldCoreCerMessage.setCerLevelId(history.getFkVipId());
        oldCoreCerMessage.setCerLevelName(StringUtils.isBlank(history.getRfVipName()) ? "" : history.getRfVipName());
        oldCoreCerMessage.setCerOccupationId(null);
        oldCoreCerMessage.setCerOccupationName(null);
        oldCoreCerMessage.setCerType(history.getVipType());
        oldCoreCerMessage.setCerMsgDate(authInfo == null ? history.getGmtCreate() : authInfo.getFirstTime());
        oldCoreCerMessage.setCerMsgVerfiy(history.getAuditTime());
        oldCoreCerMessage.setCerMsgReason(history.getAuditRemark());
        oldCoreCerMessage.setCerCopeTime(LocalDateTime.now().minusMonths(1));
        oldCoreCerMessage.setCerDeadline(calcDeadline(status, history));
        oldCoreCerMessage.setCerMsgState(status);
        oldCoreCerMessage.setCerMsgCreateDate(history.getGmtCreate());
        oldCoreCerMessageMapper.insert(oldCoreCerMessage);
        // 查询等级信息
        var oldCoreCerLevel = oldCoreCerLevelMapper.selectById(oldCoreCerMessage.getCerLevelId());
        // 新增其他信息
        var oldCoreCerLevelSub = new OldCoreCerLevelSub();
        oldCoreCerLevelSub.setCerMsgId(oldCoreCerMessage.getCerMsgId());
        oldCoreCerLevelSub.setCerLevelName(oldCoreCerLevel.getCerLevelName());
        oldCoreCerLevelSub.setCerLevelContent(oldCoreCerLevel.getCerLevelContent());
        oldCoreCerLevelSub.setCerLevelSecurity(oldCoreCerLevel.getCerLevelSecurity());
        oldCoreCerLevelSub.setCerLevelAnnual(oldCoreCerLevel.getCerLevelAnnual());
        var list = new ArrayList<String>();
        list.add(oldCoreCerLevel.getCerLevelName());
        list.add("认证");
        if (authInfo != null && authInfo.getDeposit() != null && authInfo.getDeposit() > 0) {
            if (oldCoreCerLevel.getCerLevelContent().contains("押金10000元可退")) {
                list.add("押1万");
            }
            if (oldCoreCerLevel.getCerLevelContent().contains("押金2000元可退")) {
                list.add("押2千");
            }
            if (oldCoreCerLevel.getCerLevelContent().contains("押金1000元可退")) {
                list.add("押1千");
            }
        }
        oldCoreCerLevelSub.setCerAutName(String.join(",", list));
        oldCoreCerLevelSubMapper.insert(oldCoreCerLevelSub);
    }

    /**
     * 修改的处理方法
     *
     * @param trigger           日志信息
     * @param history           认证信息
     * @param oldCoreCerMessage 旧版认证信息
     */
    @SuppressWarnings("all")
    private void update(SyncTrigger trigger, AuthenticationInfoHistory history, OldCoreCerMessage oldCoreCerMessage) {
        var status = calcStatus(history);
        var authInfo = getAuthInfoByUserId(history.getFkUserId());
        var searchId = oldCoreCerMessage.getCerSearchId();
        if (history.getVipType() == 1) {
            var person = oldCoreCerPersonMapper.selectById(searchId);
            if (person == null) {
                throw new BusinessSimpleException(String.format("未查询到个人认证信息：%s", searchId));
            }
            person.setCerUserNickname(history.getNickname());
            person.setCerPersonName(history.getName());
            person.setCerPersonPhone(history.getPhone());
            person.setCerPersonImg(history.getCertificateFront());
            person.setCerPersonIdcard(history.getCertificateNo());
            person.setCerPersonBankcard(StringUtils.isBlank(authInfo.getBankCardNo()) ? person.getCerPersonBankcard() : authInfo.getBankCardNo());
            person.setCerPersonHeadimg(history.getAvatar());
            person.setCerPersonAddress(history.getAddress());
            person.setCerPersonRange(authenticationInfoUtil.resolveBusinessTypeName(history.getRfBusinessTypeName()));
            person.setCerPersonLevel(history.getFkVipId() + "");
            person.setCerPersonCon(history.getCertificateBack());
            person.setCerPersonHand(history.getCertificateHand());
            person.setCerPersonState(status);
            oldCoreCerPersonMapper.updateById(person);
        } else if (history.getVipType() == 2) {
            var company = oldCoreCerCompanyMapper.selectById(searchId);
            if (company == null) {
                throw new BusinessSimpleException(String.format("未查询到企业认证信息：%s", searchId));
            }
            company.setCerCompanyName(history.getName());
            company.setCerCompanyNickname(history.getNickname());
            company.setCerCompanyPhone(history.getPhone());
            company.setCerCompanyImg(history.getCertificateFront());
            company.setCerCompanyIdcard(history.getCertificateNo());
            company.setCerCompanyBankcard(history.getBankCardNo());
            company.setCerCompanyHeadimg(history.getAvatar());
            company.setCerCompanyAddress(history.getAddress());
            company.setCerCompanyContact(history.getPhone());
            company.setCerCompanyRange(authenticationInfoUtil.resolveBusinessTypeName(history.getRfBusinessTypeName()));
            company.setCerCompanyLevel(history.getFkVipId() + "");
            company.setCerCompanyEdit(editCount(history.getFkUserId(), history.getId()));
            company.setCerCompanyCon(history.getCertificateBack());
            company.setCerCompanyHand(history.getCertificateHand());
            company.setCerCompanyAgent(history.getLegalName());
            company.setCerCompanyAgentPhone(history.getLegalPhone());
            company.setCerCompanyAgentFace(history.getLegalCertificateFront());
            company.setCerCompanyAgentCon(history.getLegalCertificateBack());
            company.setCerCompanyAgentHand(history.getLegalCertificateHand());
            company.setCerCompanyIdentical(history.getLegalCertificateFront().equals(history.getCertificateFront()) ? "1" : "2");
            company.setCerCompanyAgentIdcard(history.getLegalCertificateNo());
            company.setCerCompanyState(status);
            if (StringUtils.isNotBlank(history.getOtherLicense())) {
                var list = ListUtil.Spilt.to(history.getOtherLicense(), ",");
                company.setCerCompanyOther(list.size() > 0 ? list.getFirst() : "");
                company.setCerCompanyOther2(list.size() > 1 ? list.get(1) : "");
            } else {
                company.setCerCompanyOther("");
                company.setCerCompanyOther2("");
            }
            var store = getStoreByUserId(history.getFkUserId());
            if (store != null) {
                company.setCerCompanyProduct(ListUtil.Spilt.to(store.getProductUrls(), ",").stream().collect(Collectors.joining(BaseInfoConstant.IMAGE_SPILT)));
                company.setCerCompanyPremises(ListUtil.Spilt.to(store.getSceneUrls(), ",").stream().collect(Collectors.joining(BaseInfoConstant.IMAGE_SPILT)));
            }
            oldCoreCerCompanyMapper.updateById(company);
        }
        // 新增
        oldCoreCerMessage.setCerMsgNickname(history.getNickname());
        oldCoreCerMessage.setCerMsgPhone(history.getPhone());
        oldCoreCerMessage.setCerLevelId(history.getFkVipId());
        oldCoreCerMessage.setCerLevelName(history.getRfVipName());
        oldCoreCerMessage.setCerType(history.getVipType());
        oldCoreCerMessage.setCerMsgVerfiy(history.getAuditTime());
        oldCoreCerMessage.setCerMsgReason(history.getAuditRemark());
        oldCoreCerMessage.setCerMsgState(status);
        oldCoreCerMessageMapper.updateById(oldCoreCerMessage);
        // 查询等级信息
        var oldCoreCerLevel = oldCoreCerLevelMapper.selectById(oldCoreCerMessage.getCerLevelId());
        // 新增其他信息
        var oldCoreCerLevelSub = new OldCoreCerLevelSub();
        oldCoreCerLevelSub.setCerMsgId(oldCoreCerMessage.getCerMsgId());
        oldCoreCerLevelSub.setCerLevelName(oldCoreCerLevel.getCerLevelName());
        oldCoreCerLevelSub.setCerLevelContent(oldCoreCerLevel.getCerLevelContent());
        oldCoreCerLevelSub.setCerLevelSecurity(oldCoreCerLevel.getCerLevelSecurity());
        oldCoreCerLevelSub.setCerLevelAnnual(oldCoreCerLevel.getCerLevelAnnual());
        var list = new ArrayList<String>();
        list.add(oldCoreCerLevel.getCerLevelName());
        list.add("认证");
        if (authInfo.getDeposit() != null && authInfo.getDeposit() > 0) {
            if (oldCoreCerLevel.getCerLevelContent().contains("押金10000元可退")) {
                list.add("押1万");
            }
            if (oldCoreCerLevel.getCerLevelContent().contains("押金2000元可退")) {
                list.add("押2千");
            }
            if (oldCoreCerLevel.getCerLevelContent().contains("押金1000元可退")) {
                list.add("押1千");
            }
        }
        oldCoreCerLevelSub.setCerAutName(String.join(",", list));
        oldCoreCerLevelSubMapper.insert(oldCoreCerLevelSub);
    }

    /**
     * 获取上一条认证信息
     *
     * @param userId    用户id
     * @param status    状态
     * @param excludeId 排除的id
     * @return 认证信息
     */
    private AuthenticationInfoHistory getPreviousHistory(Long userId, Integer status, Long excludeId) {
        var wrapper = new LambdaQueryWrapper<AuthenticationInfoHistory>()
                .eq(AuthenticationInfoHistory::getFkUserId, userId)
                .ne(AuthenticationInfoHistory::getVipType, 0)
                .ne(excludeId != null, AuthenticationInfoHistory::getId, excludeId)
                .eq(status != null, AuthenticationInfoHistory::getAuditStatus, status)
                .orderByDesc(AuthenticationInfoHistory::getGmtCreate)
                .last("limit 1");
        return authenticationInfoHistoryMapper.selectOne(wrapper);
    }

    /**
     * 计算旧版的审核状态信息
     *
     * @param history 认证信息
     * @return 旧版的审核状态信息
     */
    private int calcStatus(AuthenticationInfoHistory history) {
        // ② 通过 数据的状态为审核通过
        if (history.getAuditStatus() == AuthInfoConstant.NewStatus.PASS) {
            return AuthInfoConstant.OldStatus.PASS;
        }
        // 查询这个用户上一条信息
        var previous = getPreviousHistory(history.getFkUserId(), null, history.getId());
        if (previous != null && previous.getId().equals(history.getId())) {
            // 修改了之前的历史记录信息
            log.debug("修改了之前的历史记录信息：{}", history.getId());
            throw new BusinessSimpleException(String.format("修改了之前的历史记录信息：%s", history.getId()));
        }
        // 判断当前的状态
        switch (history.getAuditStatus()) {
            case AuthInfoConstant.NewStatus.WAIT: {
                // ① 数据的状态为审核中，且没有查询到之前的流水数据
                if (previous == null) {
                    return AuthInfoConstant.OldStatus.AUDITING;
                }
                // ④ 数据的状态为审核中，且查询到上一条的数据为审核不通过
                if (previous.getAuditStatus() == AuthInfoConstant.NewStatus.REJECT) {
                    return AuthInfoConstant.OldStatus.REJECT_SUBMIT_AGAIN;
                }
                // 查询上一条通过的数据
                previous = getPreviousHistory(history.getFkUserId(), AuthInfoConstant.NewStatus.PASS, history.getId());
                if (previous != null) {
                    return previous.getVipType().equals(history.getVipType())
                            // ⑤ 数据的状态为审核中，且查询到上一条的数据为审核通过，且有相同的vipId
                            ? AuthInfoConstant.OldStatus.RENEWED
                            // ⑥ 数据的状态为审核中，且查询到上一条的数据为审核通过，但有不相同的vipId
                            : AuthInfoConstant.OldStatus.UPGRADE;
                }
                return AuthInfoConstant.OldStatus.AUDITING;
            }
            case AuthInfoConstant.NewStatus.REJECT: {
                // ③ 数据的状态为审核不通过，且没有查询到之前的流水数据
                if (previous == null) {
                    return AuthInfoConstant.OldStatus.REJECT;
                }
                // 查询上一条通过的数据
                previous = getPreviousHistory(history.getFkUserId(), AuthInfoConstant.NewStatus.PASS, history.getId());
                if (previous != null) {
                    return previous.getVipType().equals(history.getVipType())
                            // ⑦ 数据的状态为审核不通过，且查询到上一条的数据为审核通过，且有相同的vipId
                            ? AuthInfoConstant.OldStatus.RENEWED_REJECT
                            // ⑧ 数据的状态为审核不通过，且查询到上一条的数据为审核通过，但有不相同的vipId
                            : AuthInfoConstant.OldStatus.UPGRADE_REJECT;
                }
                return AuthInfoConstant.OldStatus.REJECT;
            }
            default:
                throw new BusinessSimpleException("未知的审核状态");
        }
    }

    /**
     * 计算到期时间
     *
     * @param status  状态
     * @param history 认证信息
     * @return 到期时间
     */
    private LocalDateTime calcDeadline(int status, AuthenticationInfoHistory history) {
        return history.getGmtCreate().plusYears(1);
    }

    /**
     * 根据用户id获取认证信息
     *
     * @param userId 用户id
     * @return 认证信息
     */
    private AuthenticationInfo getAuthInfoByUserId(Long userId) {
        var wrapper = new LambdaQueryWrapper<AuthenticationInfo>()
                .eq(AuthenticationInfo::getFkUserId, userId)
                .last("limit 1");
        return authenticationInfoMapper.selectOne(wrapper);
    }

    /**
     * 查询修改的次数信息
     *
     * @param userId    用户id
     * @param excludeId 排除的id
     * @return 修改的次数
     */
    private int editCount(Long userId, Long excludeId) {
        var wrapper1 = new LambdaQueryWrapper<AuthenticationInfoHistory>()
                .ne(AuthenticationInfoHistory::getVipType, 0)
                .ne(excludeId != null, AuthenticationInfoHistory::getId, excludeId)
                .eq(AuthenticationInfoHistory::getFkUserId, userId);
        var count1 = authenticationInfoHistoryMapper.selectCount(wrapper1);
        var wrapper2 = new LambdaQueryWrapper<AuthenticationInfoUpdate>()
                .eq(AuthenticationInfoUpdate::getFkUserId, userId);
        var count2 = authenticationInfoUpdateMapper.selectCount(wrapper2);
        return (int) (count1 + count2);
    }

    /**
     * 根据用户id获取店铺信息
     *
     * @param userId 用户id
     * @return 店铺信息
     */
    private AuthenticationInfoStore getStoreByUserId(Long userId) {
        var wrapper = new LambdaQueryWrapper<AuthenticationInfoStore>()
                .eq(AuthenticationInfoStore::getFkUserId, userId);
        return authenticationInfoStoreMapper.selectOne(wrapper);
    }

    /**
     * 名称信息
     *
     * @return 名称
     */
    @Override
    public String name() {
        return "authentication_info_history";
    }

}
